import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/common/app.dart';
import 'package:flutter_smart_heating/common/local_storage.dart';
import 'package:flutter_smart_heating/service/auth_service.dart';
import 'package:flutter_smart_heating/ui/ui.dart';
import 'package:permission_handler/permission_handler.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  String get themeName {
    var themeMode = LocalStorage.instance.getItem<int>('theme.mode') ?? 0;
    var name = '跟随系统';
    switch (themeMode) {
      case 1:
        name = '浅色模式';
        break;
      case 2:
        name = '深色模式';
        break;
    }
    return name;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('通用设置'),
      ),
      body: ListView(
        padding: const EdgeInsets.only(
          left: UiTheme.spacingX,
          right: UiTheme.spacingX,
        ),
        children: [
          titleView('基本设置'),
          UiPanel(
            children: [
              cell(
                icon: UiIcons.bell,
                title: '推送通知',
                onTap: () {
                  openAppSettings();
                },
              ),
              cell(
                icon: UiIcons.skin,
                title: '主题设置',
                showDivider: false,
                trailing: Text(themeName),
                onTap: () async {
                  await App.push('/setting/theme');
                  setState(() {});
                },
              ),
            ],
          ),
          const UiLineBreak(),
          titleView('其他信息'),
          UiPanel(
            children: [
              cell(
                icon: UiIcons.info_circle,
                title: '关于我们',
                onTap: () {
                  App.push('/setting/about');
                },
              ),
              cell(
                icon: UiIcons.mail,
                title: '反馈建议',
                onTap: () {
                  App.push('/feedback');
                },
              ),
              cell(
                icon: UiIcons.appstore,
                title: '版本更新',
                showDivider: false,
              ),
            ],
          ),
          const UiLineBreak(),
          titleView('帐号相关'),
          UiPanel(
            children: [
              cell(
                icon: UiIcons.deleteuser,
                title: '注销账户',
                onTap: () {
                  var sign = AuthService.instance.sign();
                  App.push(
                      'browser://plugin/user_destroy/index/index?title=注销账户&$sign');
                },
              ),
              cell(
                icon: UiIcons.logout,
                title: '退出登录',
                showDivider: false,
                onTap: () {
                  App.push('/user/logout');
                },
              ),
            ],
          )
        ],
      ),
    );
  }

  Widget titleView(title) {
    return Padding(
      padding: const EdgeInsets.only(left: 16, bottom: 5, top: 5),
      child: Text(
        title,
        style: const TextStyle(color: Colors.grey),
      ),
    );
  }

  Widget cell({
    required IconData icon,
    required String title,
    Function()? onTap,
    bool showDivider = true,
    Widget? trailing,
  }) {
    return UiListCell(
      onTap: onTap,
      leading: UiLinearGradientMask(
          child: Icon(
        icon,
        color: Colors.white,
      )),
      leadingSpace: 10,
      title: Text(title),
      showArrow: true,
      showDivider: showDivider,
      dividerIndent: 50,
      trailing: trailing,
      backgroundColor: Colors.transparent,
    );
  }
}
